package cn.gson.springboot.model.service;

import java.math.BigDecimal;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;

import cn.gson.springboot.model.dao.AdviceDetailsDao;
import cn.gson.springboot.model.dao.ChargeMoneyDAO;
import cn.gson.springboot.model.dao.ChargeMoneyDetailsDAO;
import cn.gson.springboot.model.dao.DoctoradviceDetailDAO;
import cn.gson.springboot.model.dao.LookillnessDAO;
import cn.gson.springboot.model.mapper.AdviceDetailsMappers;
import cn.gson.springboot.model.mapper.ChargeMoneyDetailsMapper;
import cn.gson.springboot.model.mapper.ChargeMoneyMapper;
import cn.gson.springboot.model.pojos.ChargeMoney;
import cn.gson.springboot.model.pojos.ChargemoneyDetail;
import cn.gson.springboot.model.pojos.DoctoradviceDetail;
import cn.gson.springboot.model.pojos.LookIllness;
import cn.gson.springboot.model.vo.ChargemoneyVo;

@Service
@Transactional(rollbackFor=Exception.class)
public class ChargeMoneyService {
	    //收费主表
	    @Autowired
        private ChargeMoneyMapper chmapper;
	    
	    //看诊
        @Autowired
         private LookillnessDAO ldao;
        
        //收费主表
        @Autowired
        private ChargeMoneyDAO chdao;
        
        //收费详情
        @Autowired
        private  ChargeMoneyDetailsDAO chddao;
        
        @Autowired
        private ChargeMoneyDetailsMapper chargemoneydetails;
        
        //医嘱详情
        @Autowired
	     private DoctoradviceDetailDAO  docdao;
        
        //医嘱详情的mapper层
        @Autowired
    	private AdviceDetailsMappers admapper;
	    
        /*//收费主表新增
		public void addchargemoney(Integer lookillness_id,String chargemoney_name,
																  BigDecimal chargemoney_allmoney,Integer chargemoney_type,
																  String chargemoney_people) {
			chmapper.addchargemoney(lookillness_id, chargemoney_name, chargemoney_allmoney, chargemoney_type, chargemoney_people);
			
		}*/
		
     		//药物收费
	    	public void saveDrugmoney(ChargemoneyVo chvo) {
	    		//获取收费主表
	    		ChargeMoney chargemoney = chvo.getChargemoney();
	    		
	    		//获取看诊号做外键
	    		chargemoney.setLookIllness(chvo.getLookillness());
	    		
	    		//新增收费主表
	    	   chdao.save(chargemoney);
	    		
	    		//收费详情表
	    		List<ChargemoneyDetail> chargemoneydetails = chvo.getChargemoneydetails();
			    
	    		for (ChargemoneyDetail c : chargemoneydetails) {
	    			    //详情表里面收费主表外键
					    c.setChargeMoney(chargemoney);
				       //赋值医嘱详情
					    c.setDoctoradviceDetail(c.getDoctoradviceDetail());
					    //新增收费详情
					   chddao.save(c);
					   
					 //收费成功----修改医嘱详情收费状态---1
					 admapper.updateAdviceDetailsSatus(1, c.getDoctoradviceDetail().getDocdetailsId());
				}
	    		
	    	}
	    	
	    	//修改医嘱主表收费状态
	    	public void  updateDocMoneytype( Integer docmoneytype, Integer docadviceid) {
	    		chmapper.updateDocMoneytype(docmoneytype, docadviceid);
	    	}
	    	
	    	
     		//医疗项目收费
	    	public void saveHospitalProjectmoney(ChargemoneyVo chvo) {
	    		//获取收费主表
	    		ChargeMoney chargemoney = chvo.getChargemoney();
	    		
	    		//获取看诊号做外键
	    		chargemoney.setLookIllness(chvo.getLookillness());
	    		
	    		//新增收费主表
	    	   chdao.save(chargemoney);
	    		
	    		//收费详情表
	    		List<ChargemoneyDetail> chargemoneydetails = chvo.getChargemoneydetails();
			    
	    		for (ChargemoneyDetail c : chargemoneydetails) {
	    			    //详情表里面收费主表外键
					    c.setChargeMoney(chargemoney);
				       //赋值医疗项目记录详情表
					    c.setLookHosprojectDetail(c.getLookHosprojectDetail());
					    //新增收费详情
					   chddao.save(c);
				}
	    		
	    	}
	    	
	    	//修改医疗项目收费状态
	    	public void updateHosProjectMoneytype (Integer hosplookstate, Integer hosplookid){
	    		chmapper.updateHosProjectMoneytype(hosplookstate, hosplookid);
	    	}
	    	
	    	public JSONArray selectMoney(Integer lookid) {
	    		 return chmapper.selectMoney(lookid);
	    	}

	       public void addchargedetails(ChargemoneyDetail cd) {
	    	   chddao.save(cd);
	       }
	       
	     //门诊体检收费提交
	   	public void addouthealthMoney(Integer look_id,String healthno_arr,String totalmoney,String chargename) {
		    //新增收费主表
	   	   chmapper.addchargemoney(look_id,"门诊体检收费",new BigDecimal(totalmoney), 2, chargename);
	       //查询收费主表id
		   JSONArray selectChargeMoneyid = chmapper.selectChargeMoneyid(2, look_id);
		   Integer cmid=selectChargeMoneyid.getJSONObject(0).getInteger("chargemoney_id");
		
		   //分割体检记录编号
			String arr[] =healthno_arr.split(",");
			if(!"".equals(healthno_arr)) {
				for(int i=0;i<arr.length;i++){
					//新增收费详情
					 chargemoneydetails.addchargemoneyDetails(Integer.parseInt(arr[i]), cmid,null);
					  //修改项目记录表的收费状态---改为0
					  System.out.println("体检收费完成-----------------："+arr[i]);
					   chmapper.updateHealthmoneyType(0, Integer.parseInt(arr[i]));
				}    
			}
	  }
	
	  //查出未收费人的信息
	public JSONArray  selectNoChargePeople() {
		return chmapper.selectNoChargePeople();
	}
	
	//自主体检收费
   	public void addSelfhealthMoney(String appo_no_arr,String totalmoney,String chargename) {
   	    System.out.println("service-----------appo_no_arr:"+appo_no_arr);
   		System.out.println("totalmoney--------------:"+totalmoney+"================chargename:"+chargename);
	    //新增收费主表
   	   chmapper.addchargemoney(null,"自主体检收费",new BigDecimal(totalmoney),3, chargename);
       //查询收费主表id
	   JSONArray selectSelfByid = chmapper.selectSelfByid();
	   Integer cmid=selectSelfByid.getJSONObject(0).getInteger("chargemoney_id");
	 
	   //分割体检记录编号
		String arr[] =appo_no_arr.split(",");
		if(!"".equals(appo_no_arr)) {
			for(int i=0;i<arr.length;i++){
				  //新增收费详情---预约表主键
				  chargemoneydetails.addchargemoneyDetails(null, cmid,Integer.parseInt(arr[i]));
				  //修改预约的收费状态---改为1
				 chmapper.updateHealthAppnoMoneytype(1, Integer.parseInt(arr[i]));
			}    
		}
  }	
   	
    //自主体检收费项目显示
  	public JSONArray selectSelfMoney(@Param("ccardid") String ccardid) {
  		return chmapper.selectSelfMoney(ccardid);
  	}
  	
  	//医嘱详情药物收费状态
  	public void updateAdviceDetailsSatus(Integer docdetails_type,Integer docdetailsid) {
  		admapper.updateAdviceDetailsSatus(docdetails_type, docdetailsid);
  	}
  	
    //根据看诊号与收费类型查询到收费主表id 
	public JSONArray  selectChargeMoneyid( Integer cmtype, Integer lookid) {
		return chmapper.selectChargeMoneyid(cmtype, lookid);
	}
	
	//根据看诊号与收费名称查询到收费主表id
	public JSONArray selectCMoneyid(String  cname,Integer lookid) {
		return chmapper.selectCMoneyid(cname, lookid);
	}
	
	// 自主体检收费主表id 
	public JSONArray selectCMoneyidSelf( String  chargemoney_name,Integer appo_no) {
		return chmapper.selectCMoneyidSelf(chargemoney_name, appo_no);
	}
	   	
  }
